// utils/request.js  
 
// 默认配置 
const defaultConfig = {
  baseUrl: 'https://api.tiancihunyue.com/admin/',  // 默认API地址 
  // baseUrl: '/api/',  // 默认API地址 
  timeout: 30000, // 默认超时时间 
  header: {
    'Content-Type': 'application/json' ,// 默认请求头 
	'login-token': 123
  }
}
 
// 创建请求实例 
const request = (options = {}) => {
  // 合并配置 
  const config = {
    ...defaultConfig,
    ...options.config  
  }
  
  console.log(options)
  
  // 处理URL 
  if (!options.url.startsWith('http')  && !options.url.startsWith('https'))  {
    options.url  = config.baseUrl  + options.url  
  }
  
  // 合并请求头 
  options.header  = {
    ...config.header, 
    ...options.header  
  }
  
  // 设置超时 
  options.timeout  = config.timeout  
  
  // 请求拦截器 
  request.interceptors.request(options) 
  
  return new Promise((resolve, reject) => {
    let timer = setTimeout(() => {
      reject(new Error('请求超时'))
    }, config.timeout) 
    
    uni.request({ 
      ...options,
      success: (res) => {
        clearTimeout(timer)
        request.interceptors.response(res,  resolve, reject)
      },
      fail: (err) => {
        clearTimeout(timer)
        reject(err)
      }
    })
  })
}
 
// 拦截器配置（保持不变）
request.interceptors  = {
  request: (options) => {
    // const token = uni.getStorageSync('token') 
    // if (token) {
    //   options.header  = options.header  || {}
    //   options.header['Authorization']  = 'Bearer ' + token 
    // }
    return options 
  },
  
  response: (res, resolve, reject) => {
	resolve(res.data)
    // 响应处理逻辑保持不变...
  }
}
 
export { request }